<!DOCTYPE html>
<html class="writer-html5" lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>selenium_driverless.input.utils &mdash; Selenium-Driverless 1.9.4 documentation</title>
      <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css?v=80d5e7a1" />
      <link rel="stylesheet" type="text/css" href="../../../_static/css/theme.css?v=19f00094" />

  
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../../../_static/jquery.js?v=5d32c60e"></script>
        <script src="../../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js?v=1ac0efc4"></script>
        <script src="../../../_static/doctools.js?v=888ff710"></script>
        <script src="../../../_static/sphinx_highlight.js?v=4825356b"></script>
    <script src="../../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../../../index.html" class="icon icon-home">
            Selenium-Driverless
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../api/By.html">By Element Locator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/Chrome.html">webdriver.Chrome</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/ChromeOptions.html">ChromeOptions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/Context.html">Context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/Input.html">Input</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/RequestInterception.html">Request-Interception</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/Target.html">Target</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/WebELement.html">WebElement</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Selenium-Driverless</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="../../index.html">Module code</a></li>
      <li class="breadcrumb-item active">selenium_driverless.input.utils</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for selenium_driverless.input.utils</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">selenium_driverless.types.webelement</span> <span class="kn">import</span> <span class="n">WebElement</span>
<span class="kn">from</span> <span class="nn">selenium_driverless.types</span> <span class="kn">import</span> <span class="n">JSEvalException</span>
<span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Type</span>
<span class="k">try</span><span class="p">:</span>
    <span class="kn">from</span> <span class="nn">cdp_patches.input</span> <span class="kn">import</span> <span class="n">AsyncInput</span>
    <span class="kn">from</span> <span class="nn">cdp_patches.input</span> <span class="kn">import</span> <span class="n">KeyboardCodes</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="c1"># noinspection PyTypeChecker</span>
    <span class="n">AsyncInput</span><span class="p">:</span> <span class="n">Type</span><span class="p">[</span><span class="s2">&quot;AsyncInput&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;AsyncInput&quot;</span>
    <span class="n">KeyboardCodes</span><span class="p">:</span> <span class="n">Type</span><span class="p">[</span><span class="s2">&quot;KeyboardCodes&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;KeyboardCodes&quot;</span>


<div class="viewcode-block" id="select"><a class="viewcode-back" href="../../../api/Input.html#selenium_driverless.input.utils.select">[docs]</a><span class="k">async</span> <span class="k">def</span> <span class="nf">select</span><span class="p">(</span><span class="n">elem</span><span class="p">:</span> <span class="n">WebElement</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">async_input</span><span class="p">:</span> <span class="n">AsyncInput</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                 <span class="n">timeouts</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Selects an option of a ``&lt;select&gt;`` element</span>

<span class="sd">    :param elem: ``&lt;select&gt;`` element to select an option from</span>
<span class="sd">    :param value: value for the option to select (can be different from the text!)</span>
<span class="sd">    :param text: the text of the option to select</span>
<span class="sd">    :param async_input: an instance of ``cdp_patches.input.AsyncInput``</span>
<span class="sd">    :param timeouts: timeout in seconds between actions performed to select an option</span>

<span class="sd">    .. warning::</span>
<span class="sd">        this is potentially detectable without the use of `CDP-patches &lt;https://github.com/Kaliiiiiiiiii-Vinyzu/CDP-Patches&gt;`_ !</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">use_js</span> <span class="o">=</span> <span class="n">async_input</span> <span class="ow">is</span> <span class="kc">None</span>
    <span class="k">if</span> <span class="n">use_js</span><span class="p">:</span>
        <span class="k">await</span> <span class="n">elem</span><span class="o">.</span><span class="n">click</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="k">await</span> <span class="n">elem</span><span class="o">.</span><span class="n">mid_location</span><span class="p">()</span>
        <span class="k">await</span> <span class="n">async_input</span><span class="o">.</span><span class="n">click</span><span class="p">(</span><span class="s2">&quot;left&quot;</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">text</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;value or text need to be specified&quot;</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">n</span><span class="p">,</span> <span class="n">direction</span> <span class="o">=</span> <span class="k">await</span> <span class="n">elem</span><span class="o">.</span><span class="n">execute_script</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">            let [value, text, use_js] = arguments</span>
<span class="s2">            var idx = Array.from(obj.options).findIndex(option =&gt; option.value === value || option.text === text)</span>
<span class="s2">            var currIdx = obj.selectedIndex</span>
<span class="s2">            if (idx === -1){throw ReferenceError(&quot;option not found&quot;)}</span>
<span class="s2">            if(typeof value == &#39;undefined&#39;){</span>
<span class="s2">                value = obj.options[idx].value</span>
<span class="s2">            }</span>
<span class="s2">            if(use_js &amp;&amp; obj.options[currIdx].value !== value){</span>
<span class="s2">                obj.value = value</span>
<span class="s2">                const evt = new Event(&quot;change&quot;)</span>
<span class="s2">                evt.initEvent(&quot;change&quot;, true, true)</span>
<span class="s2">                obj.dispatchEvent(evt)</span>
<span class="s2">                return [0, 1]</span>
<span class="s2">            }else{</span>
<span class="s2">                const n = Math.abs(idx - currIdx);</span>
<span class="s2">                const direction = idx &lt; currIdx ? 1 : -1</span>
<span class="s2">                return [n, direction]</span>
<span class="s2">            }</span>

<span class="s2">        &quot;&quot;&quot;</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="n">use_js</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">JSEvalException</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">class_name</span> <span class="o">==</span> <span class="s2">&quot;ReferenceError&quot;</span> <span class="ow">and</span> <span class="n">e</span><span class="o">.</span><span class="n">description</span><span class="p">[:</span><span class="mi">33</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;ReferenceError: option not found</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;option not found based on value:</span><span class="si">{</span><span class="n">value</span><span class="si">}</span><span class="s2">, text:</span><span class="si">{</span><span class="n">text</span><span class="si">}</span><span class="s2"> for </span><span class="si">{</span><span class="n">elem</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">raise</span> <span class="n">e</span>
    <span class="k">if</span> <span class="n">use_js</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">direction</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">code</span> <span class="o">=</span> <span class="n">KeyboardCodes</span><span class="o">.</span><span class="n">UP_ARROW</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">code</span> <span class="o">=</span> <span class="n">KeyboardCodes</span><span class="o">.</span><span class="n">DOWN_ARROW</span>
        <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">timeouts</span><span class="p">)</span>
            <span class="n">async_input</span><span class="o">.</span><span class="n">base</span><span class="o">.</span><span class="n">send_keystrokes</span><span class="p">(</span><span class="n">code</span><span class="p">)</span>
        <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">timeouts</span><span class="p">)</span>
        <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="k">await</span> <span class="n">elem</span><span class="o">.</span><span class="n">mid_location</span><span class="p">()</span>
        <span class="k">await</span> <span class="n">async_input</span><span class="o">.</span><span class="n">click</span><span class="p">(</span><span class="s2">&quot;left&quot;</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
        <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">timeouts</span><span class="p">)</span></div>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2024, Aurin Aegerter (aka Steve, kaliiiiiiiiii).</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>